<?php
App::uses('AppController', 'Controller');

/**
 *
 */
class LoginController extends AppController {
	public $uses = array('User');
	public function beforeFilter() {
		parent::beforeFilter();
		//$this -> Auth -> allow();
	}

	/**
	 * index method
	 */
	public function admin_index() {
		$this -> layout = false;
		//if already logged-in, redirect
		if ($this -> Session -> check('Auth.User')) {
			$this -> redirect(array('action' => 'index'));
		}

		// if we get the post information, try to authenticate
		if ($this -> request -> is('post')) {
			if ($this -> Auth -> login()) {
				$this -> Session -> write('Auth.User.lastlogin', date('m-d-Y H:i:s'));
				$uploadURL = Router::url('/') . 'app/webroot/files';
				$_SESSION['KCFINDER'] = array(
					'disabled' => false,
					'uploadURL' => $uploadURL,
					'uploadDir' => ''
				);
				$this -> redirect($this -> Auth -> redirectUrl());
			} else {
				$this -> Session -> setFlash(__('Invalid username or password'));
			}
		}
	}

	public function admin_logout() {
		$uid = $this -> Session -> read('Auth.User.id');
		$this -> loadModel('User');
		$u = $this -> Session -> read('Auth.User');
		$u['lastlogin'] = date("Y-m-d H:i:s");
		$this -> User -> save($u);
		$this -> Session -> setFlash("");
		return $this->redirect($this->Auth->logout());
	}

}
?>